<script setup>
import api from '@/api/index.js'
import { message } from 'ant-design-vue'
import { computed, ref, h, provide } from 'vue'
import MyTable from '@/components/MyTable/MyTable.vue'
import { Modal } from 'ant-design-vue'

const props = defineProps({
  // 表格数据总条数
  total: { type: Number, default: 0 },
  // 表格当前页码
  pageNum: { type: Number, default: 1 }
})
// 分页参数
const pagination = computed(() => ({
  total: props.total,
  showSizeChanger: false,
  current: props.pageNum,
  onChange: (e) => {
    emits('pageChange', e)
  }
}))

const emits = defineEmits(['edit', 'pageChange', 'reload', 'xq'])

// 表格列配置
const columns = ref([
  { ellipsis: true, title: '设备名称', dataIndex: 'deviceName', key: 'deviceName' },
  { ellipsis: true, title: '设备编号', dataIndex: 'deviceNo', key: 'deviceNo' },
  { ellipsis: true, title: '农机编号', dataIndex: 'machineNo', key: 'machineNo' },
  { ellipsis: true, title: '绑定农机', dataIndex: 'machineName', key: 'machineName' },
  { ellipsis: true, title: '当前位置', key: 'currentGpsInfo' },
  { ellipsis: true, title: '最后上报时间', key: 'v1' },
  // { ellipsis: true, title: '状态', key: 'v2', },
  { ellipsis: true, title: '更新日期', key: 'v3' },
  { ellipsis: true, title: '操作', key: 'set' }
])

// 编辑数据
const edit = (item) => {
  emits('edit', item)
}

// 数据详情
const xq = (item) => {
  emits('xq', item)
}

//删除数据
const del = (item) => {
  Modal.warning({
    title: '删除设备',
    content: h(
      'span',
      {},
      '是否删除设备数据：',
      h('span', { style: { color: '#09c690' } }, item.deviceName)
    ),
    okText: '删除',
    okCancel: true,
    okButtonProps: { type: 'primary', danger: true },
    cancelButtonProps: { type: 'primary' },
    cancelText: '取消',
    onOk: async () => {
      await api.delMachine({ id: item.deviceId })
      message.success('删除成功')
      emits('reload')
      return Promise.resolve()
    },
    onCancel: () => {
      message.success('取消删除')
      return Promise.resolve()
    }
  })
}
provide('columns', columns.value)
</script>

<template>
  <MyTable :pagination="pagination" :scroll="{ y: 'calc(100vh - 383px)' }">
    <template #bodyCell="{ column, record }">
      <template v-if="column.key === 'currentGpsInfo'">
        <span v-if="record?.currentGpsInfo?.longitude">
          {{ record?.currentGpsInfo?.longitude }},
          {{ record?.currentGpsInfo?.latitude }}
        </span>
        <span v-else> -- </span>
      </template>
      <template v-if="column.key === 'v1'">
        {{ record?.currentGpsInfo?.deviceTime }}
      </template>
      <template v-if="column.key === 'v2'">
        {{ record?.currentGpsInfo?.statusBit }}
      </template>
      <template v-if="column.key === 'v3'">
        {{ record?.updatedTime }}
      </template>
      <template v-if="column.key === 'set'">
        <div>
          <AButton type="link" @click="xq(record)">详情</AButton>
          <AButton type="link" @click="edit(record)">编辑</AButton>
          <AButton type="link" class="com-delbtn" @click="del(record)">删除</AButton>
        </div>
      </template>
    </template>
  </MyTable>
</template>